package com.youlu.campus.service.ca.impl;

import com.youlu.campus.entity.ReceiveCertificateRule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class ReceiveCertificateRuleService {

    @Autowired
    private MongoTemplate mongoTemplate;


    public Boolean defaultRule(String activityId){
        log.info("【电子证书领取规则】-> {} " ,activityId);
        if(StringUtils.isBlank(activityId)){
            return Boolean.FALSE;
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        ReceiveCertificateRule receiveCertificateRule = this.mongoTemplate.findOne(query, ReceiveCertificateRule.class);
        if(Objects.isNull(receiveCertificateRule)){
            return Boolean.TRUE;
        }
        return receiveCertificateRule.equalsDefault(receiveCertificateRule.getType());
    }

    public ReceiveCertificateRule findByAtivityId(String activityId){
      return getByAtivityId(activityId);
    }

    private ReceiveCertificateRule getByAtivityId(String activityId){
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        return this.mongoTemplate.findOne(query, ReceiveCertificateRule.class);
    }




}
